﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using NHibernateLib = NHibernate;
using YasharEl.Infrastructure.Diagnostics.Logging;

namespace YasharEl.Infrastructure.PersistenceModel.NHibernate.Diagnostics.Logging
{
    public class NHibernateLogger : NHibernateLib.IInternalLogger
    {
        #region ReadOnly Fields

        private readonly ILogger logger;

        #endregion

        #region Constructors

        public NHibernateLogger(ILogger logger)
        {
            Contract.Requires<ArgumentNullException>(logger != null, "logger could not be null.");
            this.logger = logger;
        }

        #endregion

        #region NHibernateLib.IInternalLogger Debug Methods Impelementation

        public void Debug(object message, Exception exception)
        {
            if (message != null)
            {
                logger.Debug(exception, message.ToString());
            }
            else
            {
                logger.Debug(exception, String.Empty);
            }
        }

        public void Debug(object message)
        {
            if (message != null)
            {
                logger.Debug(message.ToString());
            }
            else
            {
                logger.Debug(String.Empty);
            }
        }

        public void DebugFormat(string format, params object[] args)
        {
            logger.Debug(format, args);
        }

        #endregion

        #region NHibernateLib.IInternalLogger Error Methods Impelementation

        public void Error(object message, Exception exception)
        {
            if (message != null)
            {
                logger.Error(exception, message.ToString());
            }
            else
            {
                logger.Error(exception, String.Empty);
            }
        }

        public void Error(object message)
        {
            if (message != null)
            {
                logger.Error(message.ToString());
            }
            else
            {
                logger.Error(String.Empty);
            }
        }

        public void ErrorFormat(string format, params object[] args)
        {
            logger.Error(format, args);
        }

        #endregion

        #region NHibernateLib.IInternalLogger Fatal Methods Impelementation

        public void Fatal(object message, Exception exception)
        {
            if (message != null)
            {
                logger.Fatal(exception, message.ToString());
            }
            else
            {
                logger.Fatal(exception, String.Empty);
            }
        }

        public void Fatal(object message)
        {
            if (message != null)
            {
                logger.Fatal(message.ToString());
            }
            else
            {
                logger.Fatal(String.Empty);
            }
        }

        #endregion

        #region NHibernateLib.IInternalLogger Info Methods Impelementation

        public void Info(object message, Exception exception)
        {
            if (message != null)
            {
                logger.Info(exception, message.ToString());
            }
            else
            {
                logger.Info(exception, String.Empty);
            }
        }

        public void Info(object message)
        {
            if (message != null)
            {
                logger.Info(message.ToString());
            }
            else
            {
                logger.Info(String.Empty);
            }
        }

        public void InfoFormat(string format, params object[] args)
        {
            logger.Info(format, args);
        }

        #endregion

        #region NHibernateLib.IInternalLogger Warn Methods Impelementation

        public void Warn(object message, Exception exception)
        {
            if (message != null)
            {
                logger.Warn(exception, message.ToString());
            }
            else
            {
                logger.Warn(exception, String.Empty);
            }
        }

        public void Warn(object message)
        {
            if (message != null)
            {
                logger.Warn(message.ToString());
            }
            else
            {
                logger.Warn(String.Empty);
            }
        }

        public void WarnFormat(string format, params object[] args)
        {
            logger.Warn(format, args);
        }

        #endregion

        #region Getters

        public bool IsDebugEnabled
        {
            get { return logger.IsDebugEnabled; }
        }

        public bool IsErrorEnabled
        {
            get { return logger.IsErrorEnabled; }
        }

        public bool IsFatalEnabled
        {
            get { return logger.IsFatalEnabled; }
        }

        public bool IsInfoEnabled
        {
            get { return logger.IsInfoEnabled; }
        }

        public bool IsWarnEnabled
        {
            get { return logger.IsWarnEnabled; }
        }

        #endregion
    }
}
